package cn.com.b2office.struts2.action;

import cn.com.b2office.bean.CategoryVO;
import cn.com.b2office.domain.Category;
import cn.com.b2office.domain.Item;
import cn.com.b2office.service.CategoryService;
import cn.com.b2office.service.ItemService;
import cn.com.b2office.service.SearchService;
import cn.com.b2office.bean.Pagination;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;  
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.struts2.ServletActionContext;
import org.apache.lucene.search.highlight.*;

/**
 * @author zhenggang
 * @version Jul 24, 2008 9:43:37 AM comment search from luncene index
 */
public class SearchItemAction extends ActionSupport implements ModelDriven<Pagination> {

	private static final long serialVersionUID = 1262024769832245814L;

	private SearchService searchService = null;

	private Highlighter highlighter;
	private SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color=#C60A00>", "</font>");

	private List<Item> listSearchResult = null;
	private List<Item> listSearchResultByCatId = null;
	private List<Item> pageList = new ArrayList<Item>();
	private int maxResults = 20; // 每页显示记录数 
	private int page; // 当前页的页码
	private Pagination pagination = new Pagination(); // 分页Bean

	private CategoryService categoryService;
	private List<CategoryVO> listCat;
//	private Map<String, Integer> catCountMap = new HashMap<String,Integer>();
	private String keyword;	//搜索的关键字
	private String keywordCode;
	private String catId;
	private String style="pic";  //搜索结果的排列样式（图标、结果、文字）

	private String requestUrl;
	private HttpServletRequest request;
	
	private Logger logger;

	public String execute() throws CorruptIndexException, IOException,
			ParseException {
		logger = Logger.getLogger(SearchItemAction.class);
		
		request = ServletActionContext.getRequest();
		requestUrl = request.getRequestURI();
		keyword = new String(keyword.getBytes("ISO8859_1"),"utf-8");
		keywordCode = URLEncoder.encode(keyword,"utf8");
		logger.info("搜索的关键字是："+keyword);
		//System.out.println("搜索的关键字是："+keyword);
		listSearchResult = searchService.searchItem(keyword);
	
		pagination.init(page,listSearchResult.size(), maxResults);
		for(int j=pagination.getFirstResult(); j<pagination.getEndResult(); j++){
			Item item = listSearchResult.get(j);
			pageList.add(item);
		}
		listCat = searchService.listCatInfoByItemResult(listSearchResult);
		
		return SUCCESS;

	}
	
	public String searchByCategory() throws UnsupportedEncodingException{
		request = ServletActionContext.getRequest();
		requestUrl = request.getRequestURI();
		
		keyword = new String(keyword.getBytes("iso8859_1"),"utf-8");
		keywordCode = URLEncoder.encode(keyword,"utf8");
		//logger.info("搜索的关键字是："+keyword);
		
		listSearchResult = searchService.searchItem(keyword);
		listSearchResultByCatId = searchService.searchItemByCategory(keyword, catId);
		
		pagination.init(page,listSearchResultByCatId.size(), maxResults);
		for(int j=pagination.getFirstResult(); j<pagination.getEndResult(); j++){
			Item item = listSearchResultByCatId.get(j);
			pageList.add(item);
		}
		listCat = searchService.listCatInfoByItemResult(listSearchResult);
		
		return SUCCESS;
		
	}

	public List<Item> getListSearchResult() {
		return listSearchResult;
	}

	@Override
	public Pagination getModel() {
		// 返回分页Bean
		return pagination;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public String getRequestUrl() {
		return requestUrl;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public List<CategoryVO> getListCat() {
		return listCat;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public List<Item> getPageList() {
		return pageList;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getKeywordCode() {
		return keywordCode;
	}

	public void setCatId(String catId) {
		this.catId = catId;
	}

	public String getCatId() {
		return catId;
	}
}
